package com.fr.web.core.service;

import com.fr.base.CodeUtils;
import com.fr.base.FRContext;
import com.fr.base.StringUtils;
import com.fr.base.core.json.JSONArray;
import com.fr.base.core.json.JSONObject;
import com.fr.base.core.util.TemplateUtils;
import com.fr.base.core.util.vt.VT4FR;
import com.fr.base.core.util.vt.VT4FR.FUNC;
import com.fr.base.dav.Env;
import com.fr.base.file.DatasourceManager;
import com.fr.data.TableData;
import com.fr.data.core.db.TableProcedure;
import com.fr.data.impl.ADHOCNamedTableData;
import com.fr.data.impl.ADHOCSQLTableData;
import com.fr.data.impl.ADHOCTableData;
import com.fr.data.impl.DatabaseConnection;
import com.fr.web.core.WebUtils;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ADHOCReportService extends NoSessionIDService
{
  public static String KEY = "adhoc";
  public static String ADHOCDATA = "adhoc_dataset";
  public static String ADHOCDESIGN = "adhoc_design";
  public static String GETCONNECTIONSNAME = "adhoc_getconnectionsname";
  public static String GETDBDATASNAME = "adhoc_getdbdatasname";
  public static String GETDBDATATABLES = "adhoc_getdbdatatables";
  public static String GETTABLEFIELDS = "adhoc_gettablefields";
  public static String ADDVIEW = "adhoc_addview";
  public static String GETVIEW = "adhoc_getview";
  public static String GETVIEWSNAME = "adhoc_getviewsname";
  public static String EDITVIEW = "adhoc_editview";
  public static String GETVIEWPARTDATA = "adhoc_getviewpartdata";
  public static String GETVIEWDESCRIPTION = "adhoc_getviewdescription";
  public static String GETVIEWFIELDS = "adhoc_getviewfields";
  public static String GENERATEREPORT = "adhoc_generatereport";
  private static ADHOCReportService ADHOCSERVICE = new ADHOCReportService();

  public static ADHOCReportService getInstance()
  {
    return ADHOCSERVICE;
  }

  public void process(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, String paramString)
    throws Exception
  {
    if (ADHOCDATA.equals(paramString))
      generateADHOCData(paramHttpServletRequest, paramHttpServletResponse);
    else if (ADHOCDESIGN.equals(paramString))
      generateADHOCDesign(paramHttpServletRequest, paramHttpServletResponse);
    else if ("adhoc_getdatanames".equals(paramString))
      dealADHOCDataNames(paramHttpServletRequest, paramHttpServletResponse);
    else if ("adhoc_adddata".equals(paramString))
      addADHOCData(paramHttpServletRequest, paramHttpServletResponse);
    else if ("adhoc_getdata".equals(paramString))
      dealGetADHOCData(paramHttpServletRequest, paramHttpServletResponse);
    else if ("adhoc_editdata".equals(paramString))
      dealEditADHOCData(paramHttpServletRequest, paramHttpServletResponse);
    else if ("adhoc_removedata".equals(paramString))
      dealRemoveADHOCData(paramHttpServletRequest);
    else if ("adhoc_getdatasourcenames".equals(paramString))
      dealGetDataNames(paramHttpServletRequest, paramHttpServletResponse);
    else if ("adhoc_getdetail".equals(paramString))
      dealGetDataDetail(paramHttpServletRequest, paramHttpServletResponse);
    else if ("adhoc_getdatafields".equals(paramString))
      dealGetDataFields(paramHttpServletRequest, paramHttpServletResponse);
    else if ("adhoc_design_viewlist".equals(paramString))
      dealGetViewList(paramHttpServletRequest, paramHttpServletResponse);
    else if ("adhoc_gettabledatanames".equals(paramString))
      dealGetTableDataNames(paramHttpServletRequest, paramHttpServletResponse);
    else if ("adhoc_gettablenames".equals(paramString))
      dealGetTableNames(paramHttpServletRequest, paramHttpServletResponse);
  }

  private static void dealEditADHOCData(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String str1 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "oldname");
    String str2 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "newname");
    ADHOCTableData localADHOCTableData = (ADHOCTableData)FRContext.getDatasourceManager().getADHOC(str1);
    FRContext.getDatasourceManager().removeADHOC(str1);
    FRContext.getDatasourceManager().putADHOC(str2, localADHOCTableData);
    FRContext.getCurrentEnv().writeResource(FRContext.getDatasourceManager());
    dealPrinter(paramHttpServletResponse, nameADHOCDataString(localADHOCTableData, str2));
  }

  private static void dealGetTableNames(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String str = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "datalink");
    JSONArray localJSONArray = new JSONArray();
    if (StringUtils.isNotEmpty(str))
    {
      DatabaseConnection localDatabaseConnection = (DatabaseConnection)FRContext.getDatasourceManager().getConnection(str);
      TableProcedure[] arrayOfTableProcedure = FRContext.getCurrentEnv().getSQLTablesProcedure(localDatabaseConnection, null);
      int i = 0;
      int j = arrayOfTableProcedure.length;
      while (i < j)
      {
        if (arrayOfTableProcedure[i].getType() == "PROCEDURE")
          break label116:
        localJSONArray.put(new JSONObject().put("text", arrayOfTableProcedure[i].getName()).put("value", arrayOfTableProcedure[i].getName()));
        label116: ++i;
      }
    }
    dealPrinter(paramHttpServletResponse, CodeUtils.jsonEncode(localJSONArray));
  }

  private static void dealGetTableDataNames(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    JSONArray localJSONArray = new JSONArray();
    Iterator localIterator = FRContext.getDatasourceManager().getTableDataNameIterator();
    while (localIterator.hasNext())
    {
      String str = (String)localIterator.next();
      localJSONArray.put(new JSONObject().put("value", str).put("text", str));
    }
    dealPrinter(paramHttpServletResponse, CodeUtils.jsonEncode(localJSONArray));
  }

  private static void dealADHOCReport(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
  }

  private static void dealGetViewList(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String str = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "viewname");
    ADHOCTableData localADHOCTableData = (ADHOCTableData)FRContext.getDatasourceManager().getADHOC(str);
    JSONArray localJSONArray = new JSONArray();
    int i = 0;
    int j = localADHOCTableData.getColumnCount();
    while (i < j)
    {
      localJSONArray.put(localADHOCTableData.getColumnName(i));
      ++i;
    }
    dealPrinter(paramHttpServletResponse, CodeUtils.jsonEncode(localJSONArray));
  }

  private static void dealGetDataFields(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String str = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "viewname");
    ADHOCTableData localADHOCTableData = (ADHOCTableData)FRContext.getDatasourceManager().getADHOC(str);
    JSONArray localJSONArray = new JSONArray();
    int i = 0;
    int j = localADHOCTableData.getColumnCount();
    while (i < j)
    {
      localJSONArray.put(new JSONObject().put("value", localADHOCTableData.getColumnName(i)).put("text", localADHOCTableData.getColumnName(i)));
      ++i;
    }
    dealPrinter(paramHttpServletResponse, CodeUtils.jsonEncode(localJSONArray));
  }

  private static void dealGetDataDetail(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String str = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "dataname");
    ADHOCTableData localADHOCTableData = (ADHOCTableData)FRContext.getDatasourceManager().getADHOC(str);
    JSONObject localJSONObject = new JSONObject();
    JSONArray localJSONArray1 = new JSONArray();
    int i = (localADHOCTableData.getRowCount() > 5) ? 5 : localADHOCTableData.getRowCount();
    int j = 0;
    int k = localADHOCTableData.getColumnCount();
    while (j < k)
    {
      JSONArray localJSONArray2 = new JSONArray();
      localJSONArray2.put(localADHOCTableData.getColumnName(j));
      for (int l = 0; l < i; ++l)
        localJSONArray2.put(localADHOCTableData.getValueAt(l, j));
      localJSONArray1.put(localJSONArray2);
      ++j;
    }
    localJSONObject.put("description", localADHOCTableData.getDescription());
    localJSONObject.put("tabledata", localJSONArray1);
    localADHOCTableData.release();
    dealPrinter(paramHttpServletResponse, CodeUtils.jsonEncode(localJSONObject));
  }

  private static void dealGetDataNames(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    JSONArray localJSONArray = new JSONArray();
    Iterator localIterator = FRContext.getDatasourceManager().getConnectionNameIterator();
    while (localIterator.hasNext())
    {
      String str = (String)localIterator.next();
      localJSONArray.put(new JSONObject().put("value", str).put("text", str));
    }
    dealPrinter(paramHttpServletResponse, CodeUtils.jsonEncode(localJSONArray));
  }

  private static void dealADHOCDataNames(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    Iterator localIterator = FRContext.getDatasourceManager().getADHOCNameIterator();
    JSONArray localJSONArray = new JSONArray();
    while (localIterator.hasNext())
      localJSONArray.put(localIterator.next());
    dealPrinter(paramHttpServletResponse, localJSONArray.toString());
  }

  private static void addADHOCData(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String str1 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "namedata");
    JSONObject localJSONObject = new JSONObject(str1);
    String str2 = null;
    if (localJSONObject.get("text") != null)
      str2 = localJSONObject.getString("text");
    if (StringUtils.isEmpty(str2))
    {
      dealFailedStatePrinter(paramHttpServletResponse);
      return;
    }
    Object localObject = null;
    if (localJSONObject.get("type") != null)
    {
      if (localJSONObject.getInt("type") == ADHOCSQLTableData.TYPE)
        localObject = new ADHOCSQLTableData();
      else if (localJSONObject.getInt("type") == ADHOCNamedTableData.TYPE)
        localObject = new ADHOCNamedTableData();
      ((ADHOCTableData)localObject).parseJSON(localJSONObject);
      DatasourceManager localDatasourceManager = FRContext.getDatasourceManager();
      localDatasourceManager.putADHOC(str2, (TableData)localObject);
      FRContext.getCurrentEnv().writeResource(localDatasourceManager);
    }
    dealPrinter(paramHttpServletResponse, "success");
  }

  private static void dealGetADHOCData(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    DatasourceManager localDatasourceManager = FRContext.getDatasourceManager();
    String str = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "dataname");
    ADHOCTableData localADHOCTableData = (ADHOCTableData)localDatasourceManager.getADHOC(str);
    if (localADHOCTableData == null)
    {
      dealFailedStatePrinter(paramHttpServletResponse);
      return;
    }
    dealPrinter(paramHttpServletResponse, nameADHOCDataString(localADHOCTableData, str));
  }

  private static String nameADHOCDataString(ADHOCTableData paramADHOCTableData, String paramString)
    throws Exception
  {
    JSONObject localJSONObject = paramADHOCTableData.createJSON();
    localJSONObject.put("text", paramString);
    return localJSONObject.toString();
  }

  private static void dealRemoveADHOCData(HttpServletRequest paramHttpServletRequest)
    throws Exception
  {
    DatasourceManager localDatasourceManager = FRContext.getDatasourceManager();
    String str = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "dataname");
    localDatasourceManager.removeADHOC(str);
  }

  private static void dealFailedStatePrinter(HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    dealPrinter(paramHttpServletResponse, "false");
  }

  private static void dealPrinter(HttpServletResponse paramHttpServletResponse, String paramString)
    throws Exception
  {
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    localPrintWriter.print(paramString);
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  private static void generateADHOCData(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    Map localMap = WebUtils.createTemplateMap(paramHttpServletRequest);
    TemplateUtils.dealWithTemplate("/com/fr/web/adhoc/html/adhocdata.html", paramHttpServletResponse, localMap);
  }

  private static void generateADHOCDesign(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    Map localMap = WebUtils.createTemplateMap(paramHttpServletRequest);
    TemplateUtils.dealWithTemplate("/com/fr/web/adhoc/html/adhocdesign.html", paramHttpServletResponse, localMap);
  }

  public boolean accept(String paramString)
  {
    return ((VT4FR.ADHOC.support()) && (paramString.startsWith(KEY)));
  }
}